<?php

namespace app\controller;

use think\facade\Cache;

class AdminUsers
{
    public function signOut()
    {
        Cache::clear();
        return redirect('/');
    }

    public static function getCount(\app\model\AdminUsers $model)
    {
        return $model->getCount();
    }

    public static function read(\app\model\AdminUsers $model)
    {
        return $model->read();
    }

    public function signIn(\app\model\AdminUsers $model)
    {
        $model->account = input('param.account');
        $model->password = get_encode_password(input('param.password'));
        return boolval($model->signIn()) == true ?
            sign_in_follow_operation($model->signIn()) : json(500);
    }

    public function updateUserInfo(\app\model\AdminUsers $model)
    {
        $model->password = get_encode_password(input('param.password'));
        return boolval($model->updateUserInfo()) == true ?
            update_user_info_follow_operation() : json(500);
    }

    public function deleteAdminUser(\app\model\AdminUsers $model)
    {
        $model->id = input('param.id');
        record_log('删除编号为 '.input('param.id').' 的管理员');
        return boolval($model->deleteAdminUser()) == true ?
            json(200) : json(500);
    }

    public function disable(\app\model\AdminUsers $model)
    {
        $model->id = input('param.id');
        $model->disableState = 1;
        record_log('禁用编号为 '.input('param.id').' 的管理员');
        return boolval($model->updateDisableState()) == true ?
            json(200) : json(500);
    }

    public function unDisable(\app\model\AdminUsers $model)
    {
        $model->id = input('param.id');
        $model->disableState = 0;
        record_log('启用编号为 '.input('param.id').' 的管理员');
        return boolval($model->updateDisableState()) == true ?
            json(200) : json(500);
    }

    public function addAdminUser(\app\model\AdminUsers $adminUsers, \app\model\AdminUsersRole $adminUsersRole)
    {
        $adminUsers->account = input('param.account');
        $adminUsers->password = get_encode_password(input('param.password'));
        $roleId = implode('|', input('param.role_id'));
        $adminId = $adminUsers->addAdminUser();
        $adminUsersRole->roleId = $roleId;
        $adminUsersRole->adminId = $adminId;
        $adminUsersRole->addAdminUserRole();
        record_log('增加管理员 '.input('param.account'));
        return json(200);
    }

    public function editAdminUser(\app\model\AdminUsers $adminUsers, \app\model\AdminUsersRole $adminUsersRole)
    {
        $adminUsers->id = input('param.id');
        $adminUsers->account = input('param.account');
        $adminUsers->password = get_encode_password(input('param.password'));
        $roleId = implode('|', input('param.role_id'));
        $adminUsers->editAdminUser();
        $adminUsersRole->adminId = input('param.id');
        $adminUsersRole->roleId = $roleId;
        $adminUsersRole->editAdminUserRole();
        record_log('编辑编号为 '.input('param.id').' 的管理员');
        return json(200);
    }

    public static function queryData(\app\model\AdminUsers $adminUsers, int $id)
    {
        $adminUsers->id = $id;
        return $adminUsers->queryData();
    }
}